#include "hgeflash.h"

#include "FlashPlayer.h";

HGE *hgeFlash::hge = 0;

hgeFlash::hgeFlash(void) {
	hge = hgeCreate(HGE_VERSION);
	Player = 0;
	spr = 0;
}

hgeFlash::~hgeFlash(void) {
	if(Player) delete Player;
	if(spr) {
		if(spr->GetTexture()) hge->Texture_Free(spr->GetTexture());
	}
	hge->Release();
}

bool hgeFlash::Start(const HGE_CHAR *filename, float x, float y, float width, float height) {
	if(spr) {
		if(spr->GetTexture()) hge->Texture_Free(spr->GetTexture());
		delete spr;
		spr = 0;
	}

	HTEXTURE tex = hge->Texture_Create(width, height);
	if(tex == NULL) {
		hge->System_Log(TXT("Flash texture not created."));
		return false;
	}

	X = x; Y = y;
	Width = (float)width;
	Height = (float)height;

	spr = new hgeSprite(tex, 0, 0, Width, Height);

	if(!Player) {
		Player = new FlashPlayer;
		if(!Player) {
			hge->System_Log(TXT("FlashPlayer not created."));
			return false;
		}
	}

	Player->StartAnimation(hge->Resource_MakePath(filename), Width, Height, 0);
	Player->SetQuality(FlashPlayer::QUALITY_MEDIUM);
	Player->GotoFrame(0);

	return true;
}

void hgeFlash::SetQuality(int byQuality) {
	if(Player) {
		if(byQuality == HGEFLASH_LOW)
			Player->SetQuality(FlashPlayer::QUALITY_LOW);
		else if(byQuality == HGEFLASH_MEDIUM)
			Player->SetQuality(FlashPlayer::QUALITY_MEDIUM);
		else
			Player->SetQuality(FlashPlayer::QUALITY_HIGH);
	}
}

bool hgeFlash::IsPlaying() {
	if(Player)
		return Player->IsPlaying() == true;
	return false;
}

void hgeFlash::Pause() {
	if(Player)
		Player->Pause();
}

void hgeFlash::Unpause() {
	if(Player)
		Player->Unpause();

}

void hgeFlash::Back() {
	if(Player)
		Player->Back();

}

void hgeFlash::Rewind() {
	if(Player)
		Player->Rewind();

}

void hgeFlash::Forward() {
	if(Player)
		Player->Forward();
}

void hgeFlash::GotoFrame(int nFrame) {
	if(Player)
		Player->GotoFrame(nFrame);

}

int hgeFlash::GetCurrentFrame() {
	if(Player)
		return Player->GetCurrentFrame();
	return -1;
}

int hgeFlash::GetTotalFrames() {
	if(Player)
		return Player->GetTotalFrames();
	return -1;
}

bool hgeFlash::GetLoopPlay() {
	if(Player)
		return Player->GetLoopPlay();
	return false;
}

void hgeFlash::SetLoopPlay(bool bLoop) {
	if(Player)
		Player->SetLoopPlay(bLoop);
}

void hgeFlash::Update(void) {
	if(Player) {
		Player->Update();
		if(Player->Render()) {
			int tex_w = hge->Texture_GetWidth(spr->GetTexture());

			int nWidth = Player->GetWidth();
			int nHeight = Player->GetHeight();

			DWORD* dwFlashs = (DWORD*)Player->GetFlashFrameBuffer();
			DWORD* pixels = hge->Texture_Lock(spr->GetTexture(), false, 0, 0, nWidth, nHeight);

			for(int y = 0; y < nHeight; y++) 
				memcpy(&pixels[y * tex_w], &dwFlashs[y * nWidth], sizeof(DWORD) * nWidth);

			hge->Texture_Unlock(spr->GetTexture());
		}
		hgeInputEvent ev;
		if(hge->Input_GetEvent(&ev)) {
			if(ev.type == INPUT_MOUSEMOVE) Player->MouseMove(ev.x - X, ev.y - Y);
			if(ev.type == INPUT_MBUTTONDOWN && ev.key == HGEK_LBUTTON) {
				Player->MouseLButtonDown(ev.x - X, ev.y - Y);
			}
			if(ev.type == INPUT_MBUTTONUP && ev.key == HGEK_LBUTTON) {
				Player->MouseLButtonUp(ev.x - X, ev.y - Y);
			}
		}
	}
}

void hgeFlash::Render(void) {
	spr->RenderStretch(X, Y, X + Width, Y + Height);
}
